package com.graduation.sfstore.controller;

import com.graduation.sfstore.entity.*;
import com.graduation.sfstore.event.EventProducer;
import com.graduation.sfstore.service.StoreService;
import com.graduation.sfstore.util.HostHolder;
import com.graduation.sfstore.util.SFWarehouseConstant;
import com.graduation.sfstore.util.SFWarehouseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.*;

@Controller
public class StoreController implements SFWarehouseConstant {

    @Autowired
    private StoreService storeService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private EventProducer eventProducer;


    @RequestMapping(path = "/addstoreA",method = RequestMethod.GET)
    public String getAllStorePage(Model model, Page page){
        page.setRows(storeService.selectStoreRows(1));
        page.setPath("/addstoreA");

        List<Store> list = storeService.findAllStores(1,page.getOffset(),page.getLimit());
        List<Map<String, Object>> stores = new ArrayList<Map<String, Object>>();
        if (list!=null){
            for (Store store:list) {
                Map<String, Object> map = new HashMap<>();
                map.put("store",store);
                stores.add(map);
            }
        }
        model.addAttribute("stores",stores);

        return "/inoutstores";
    }

    @RequestMapping(path = "/addstoreB",method = RequestMethod.GET)
    public String getAllStorePageB(Model model, Page page){
        page.setRows(storeService.selectStoreRows(2));
        page.setPath("/addstoreB");

        List<Store> list = storeService.findAllStores(2,page.getOffset(),page.getLimit());
        List<Map<String, Object>> stores = new ArrayList<Map<String, Object>>();
        if (list!=null){
            for (Store store:list) {
                Map<String, Object> map = new HashMap<>();
                map.put("store",store);
                stores.add(map);
            }
        }
        model.addAttribute("stores",stores);

        return "/inoutstores";
    }

    @RequestMapping(path = "/addstoreC",method = RequestMethod.GET)
    public String getAllStorePageC(Model model, Page page){
        page.setRows(storeService.selectStoreRows(3));
        page.setPath("/addstoreC");

        List<Store> list = storeService.findAllStores(3,page.getOffset(),page.getLimit());
        List<Map<String, Object>> stores = new ArrayList<Map<String, Object>>();
        if (list!=null){
            for (Store store:list) {
                Map<String, Object> map = new HashMap<>();
                map.put("store",store);
                stores.add(map);
            }
        }
        model.addAttribute("stores",stores);

        return "/inoutstores";
    }

    @RequestMapping(path = "/addstoreD",method = RequestMethod.GET)
    public String getAllStorePageD(Model model, Page page){
        page.setRows(storeService.selectStoreRows(4));
        page.setPath("/addstoreD");

        List<Store> list = storeService.findAllStores(4,page.getOffset(),page.getLimit());
        List<Map<String, Object>> stores = new ArrayList<Map<String, Object>>();
        if (list!=null){
            for (Store store:list) {
                Map<String, Object> map = new HashMap<>();
                map.put("store",store);
                stores.add(map);
            }
        }
        model.addAttribute("stores",stores);

        return "/inoutstores";
    }


    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    @RequestMapping(path="/add",method = RequestMethod.POST)
    public String addStore(Model model, String id, String fishname, String weight, String location) {
        User user = hostHolder.getUser();
        if (user == null) {
            return SFWarehouseUtil.getJSONString(403, "你还没有登录哦!");
        }
        SpeicesSort storefindname = storeService.findStores(fishname);
        if (storefindname==null) {
            return "505";
        }
        Store store = new Store();
        store.setFishname(fishname);
        store.setWeight(Double.parseDouble(weight));
        Date date = new Date();
        store.setCreateTime(date);
        store.setLocation(location);
        store.setRegionid(storefindname.getRegionid());
        store.setRegion(storefindname.getRegion());
        store.setUserid(user.getId());
        store.setToid(1);
        store.setType(1);
        if (!StringUtils.isBlank(id)) {
            System.out.println(fishname);
            //更新总库
            int updatefishstore = storeService.updatefishstore(Integer.parseInt(id), Double.parseDouble(weight), 1);
            //入分库
            int i = storeService.insertfishstorein(store);
        }else {
            //入总库
            int i = storeService.insertfishstore(store);
            //入分库
            i = storeService.insertfishstorein(store);

        }

        //触发入库事件
        Event event = new Event()
                .setTopic( TOPIC_INWAREHOUSE)
                .setUserId(hostHolder.getUser().getId())
                .setToid(1)
                .setFishname(fishname)
                .setWeight(Double.parseDouble(weight))
                .setDate(date);
        eventProducer.fireEvent(event);

        if (storefindname.getRegionid() == 1) {
            return "redirect:/addstoreA";
        }
        if (storefindname.getRegionid() == 2) {
            return "redirect:/addstoreB";
        }
        if (storefindname.getRegionid() == 3) {
            return "redirect:/addstoreC";
        } else {
            return "redirect:/addstoreD";
        }
    }

    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    @RequestMapping(path="/out",method = RequestMethod.POST)
    public String outStore(Model model, int id, String fishname, String weight) {
        User user = hostHolder.getUser();
        if (user == null) {
            return SFWarehouseUtil.getJSONString(403, "你还没有登录哦!");
        }
        SpeicesSort storefindname = storeService.findStores(fishname);
        if (storefindname==null){
            return "505";
        }
        Date date = new Date();

        int i = storeService.updatefishstore(id,Double.parseDouble(weight),0);
        if (i>0){
            Store store = new Store();
            store.setUserid(user.getId());
            store.setToid(1);
            store.setRegionid(storefindname.getRegionid());
            store.setRegion(storefindname.getRegion());
            store.setFishname(fishname);
            store.setWeight(Double.parseDouble(weight));

            store.setCreateTime(date);
            int i1 = storeService.insertfishstoreout(store);
            if (i1>0){
                model.addAttribute("storeMsg2","操作成功");
            }else {
                return "505";
            }
        }else {
            return "505";
        }

        //触发出货通知
        Event event = new Event()
                .setTopic( TOPIC_OUTWAREHOUSE)
                .setUserId(hostHolder.getUser().getId())
                .setToid(1)
                .setFishname(fishname)
                .setDate(date)
                .setWeight(Double.parseDouble(weight));
        eventProducer.fireEvent(event);



        if (storefindname.getRegionid()==1)
        {
            return "redirect:/addstoreA";
        }
        if (storefindname.getRegionid()==2){
            return "redirect:/addstoreB";
        }
        if (storefindname.getRegionid()==3){
            return "redirect:/addstoreC";
        }
        else {
            return "redirect:/addstoreD";
        }
    }


    @RequestMapping(path = "/chart", method = RequestMethod.GET)
    public String getChartPage(Model model) {

        int Acount = storeService.findMainAmountByName("A");
        int Bcount = storeService.findMainAmountByName("B");
        int Ccount = storeService.findMainAmountByName("C");
        int Dcount = storeService.findMainAmountByName("D");
        int sum = Acount + Bcount + Ccount + Dcount;
        Acount = (int)((double)Acount/(double)sum*100);
        Bcount = (int)((double)Bcount/(double)sum*100);
        Ccount = (int)((double)Ccount/(double)sum*100);
        Dcount = (int)((double)Dcount/(double)sum*100);
        model.addAttribute("Acount",Acount);
        model.addAttribute("Bcount",Bcount);
        model.addAttribute("Ccount",Ccount);
        model.addAttribute("Dcount",Dcount);


        //入库重量
        int instoreA = (int)storeService.findRegionWight("A");
        int instoreB = (int)storeService.findRegionWight("B");
        int instoreC = (int)storeService.findRegionWight("C");
        int instoreD = (int)storeService.findRegionWight("D");
        model.addAttribute("instoreA",instoreA);
        model.addAttribute("instoreB",instoreB);
        model.addAttribute("instoreC",instoreC);
        model.addAttribute("instoreD",instoreD);


        //出库重量
        int outstoreA = (int)storeService.findRegionOutWight("A");
        int outstoreB = (int)storeService.findRegionOutWight("B");
        int outstoreC = (int)storeService.findRegionOutWight("C");
        int outstoreD = (int)storeService.findRegionOutWight("D");
        model.addAttribute("outstoreA",outstoreA);
        model.addAttribute("outstoreB",outstoreB);
        model.addAttribute("outstoreC",outstoreC);
        model.addAttribute("outstoreD",outstoreD);



        return "/charts";
    }



}
